using System.Collections;
using System.Collections.Generic;
using UnityEngine;



public class Item : MonoBehaviour
{
    //public GameObject text;

    private float _time;
    public int ranges;

    public string propName;
    public int propNum;

    public Rigidbody rb;

    public GameObject effectsPrefab;

    private PlayerCharacterController player;

    private bool isGrouned;
    // Start is called before the first frame update
    void Start()
    {
        player = GameObject.FindWithTag("Player").GetComponent<PlayerCharacterController>();
        _time = Time.time;
        rb.AddForce(0, 50, 0);
    }

    // Update is called once per frame
    void Update()
    {
        transform.RotateAround(transform.position, transform.up, 0.5f);

        Vector3 target = player.transform.position + new Vector3(0, 1, 0);

        if (Time.time - _time > 60)
            Destroy(gameObject);

        if ((target - transform.position).sqrMagnitude < ranges * ranges)
        {
            transform.position = Vector3.MoveTowards(transform.position, target, 0.2f);
            if ((target - transform.position).sqrMagnitude < 1)
            {
                GameData.AddProp(propName, propNum);
                Destroy(gameObject);
            }
        }
    }

    private void OnTriggerEnter(Collider other)
    {
        if (isGrouned == false)
        {
            if (other.gameObject.layer == 6)
            {
                OnGround();
            }
        }
    }

    private void OnGround()
    {
        rb.useGravity = false;
        rb.isKinematic = true;
        isGrouned = true;
        GetComponent<SphereCollider>().enabled = false;
    }
}
